package com.hxd.charitysystem.ui.activity

import android.app.Dialog
import com.hjq.dialog.PayPasswordDialog
import com.hjq.dialog.WaitDialog
import com.hxd.charitysystem.R
import com.hxd.charitysystem.base.common.MyActivity
import com.hxd.charitysystem.bean.NetBaseBean
import com.hxd.charitysystem.bean.UserInfoBean
import com.hxd.charitysystem.utils.L
import com.hxd.charitysystem.utils.ProjectUrl
import com.hxd.tabactivityfragment.util.GsonToBean
import com.kongzue.dialog.v3.InputDialog
import com.kymjs.rxvolley.client.HttpCallback
import com.kymjs.rxvolley.client.HttpParams
import com.vondear.rxtool.RxSPTool
import kotlinx.android.synthetic.main.activity_user_recharge.*

/**
 *  项目名：    CharitySystem
 *  包名：      com.hxd.charitysystem.ui.activity
 *  文件名:     UserRechargeActivity
 *  创建者:     HXD
 *  创建时间:   2019/5/5 10:10
 *  描述:       用户余额充值界面
 */

class UserRechargeActivity : MyActivity() {

    private val token by lazy { RxSPTool.getString(this, "token") }
    private val rechargeWaitDialog by lazy { WaitDialog.Builder(this).setMessage("正在充值中...").show(); }
    private val userInfoWaitDialog by lazy { WaitDialog.Builder(this).setMessage("正在获取数据...").show(); }
    private var thisAmount:Float = 0.0f

    override fun getLayoutId(): Int {
        return R.layout.activity_user_recharge
    }

    override fun getTitleId(): Int {
        return 0
    }

    override fun initView() {
        iv_right_back.setOnClickListener {
            finish()
        }

        /**
         * 立即充值按钮
         */
        btn_recharge.setOnClickListener {
            InputDialog.show(this, "余额充值", "请输入充值金额", "确定", "取消")
                .setOnOkButtonClickListener { baseDialog, v, inputStr ->
                    PayPasswordDialog.Builder(this)
                        .setTitle("请输入支付密码")
                        .setSubTitle("余额充值")
                        .setMoney(inputStr.toString())
                        .setAutoDismiss(true) // 设置点击按钮后不关闭对话框
                        .setListener(object : PayPasswordDialog.OnListener {
                            override fun onCompleted(dialog: Dialog, password: String) {
                                /**
                                 * 暂时不用验证支付密码
                                 */

                                recharge(inputStr.toString().toInt())
                            }

                            override fun onCancel(dialog: Dialog) {
                                toast("取消了")
                            }
                        })
                        .show()
                    false
                }

        }
    }

    override fun initData() {
        loadData()
    }

    /**
     * 进行充值
     */
    private fun recharge(amount: Int) {
        val params = HttpParams()
        params.putHeaders("token", token)
        params.put("amount", amount)
        rechargeWaitDialog
        ProjectUrl.postUserCharge(params, object : HttpCallback() {
            override fun onSuccess(result: String?) {
                val data = GsonToBean.JsonToBean<NetBaseBean<String>>(result)
                rechargeWaitDialog.dismiss()
                if (data.status == 200) {
                    tv_amount_recharge.text = (amount + thisAmount.toInt()).toString()
                    toast("充值成功")
                } else {
                    L.i("充值失败 ${data.msg}")
                }
            }

            override fun onFailure(errorNo: Int, strMsg: String?) {
                rechargeWaitDialog.dismiss()
                L.i(" 充值失败 : $errorNo, strMsg: $strMsg ")
            }
        })
    }

    /**
     * 加载用户信息
     */
    private fun loadData(){
        val params = HttpParams()
        params.putHeaders("token", token)
        userInfoWaitDialog
        ProjectUrl.getUserInfo(params, object: HttpCallback(){
            override fun onSuccess(t: String?) {
                val userInfo = GsonToBean.JsonToBean<UserInfoBean>(t)
                userInfoWaitDialog.dismiss()
                if(userInfo.status == 200){
                    thisAmount = userInfo.msg.balance
                    tv_amount_recharge.text = userInfo.msg.balance.toString()
                    tv_userName_recharge.text = userInfo.msg.name
                }else{
                    userInfoWaitDialog.dismiss()
                    toast("获取用户数据失败 ${userInfo.msg}")
                }
            }

            override fun onFailure(errorNo: Int, strMsg: String?) {
                userInfoWaitDialog.dismiss()
                L.i(" errorNo : $errorNo, strMsg: $strMsg ")
            }
        })
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }
}